package com.hnevc.listviewdemo;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {

    private static final String TAG = "LoginActivity";
    Button mBtnLogin;
    Button mBtnSaveIP;//定义按钮变量
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        //从布局文件中获取按钮对象
        mBtnSaveIP = (Button) findViewById(R.id.id_btn_saveip);

        //设置按钮对象的点击事件监听者
        mBtnSaveIP.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //保存服务器IP到本地sharepreference中
                //1 获取ip
                EditText etIp = (EditText) findViewById(R.id.id_et_ip);
                String ip = etIp.getText().toString();
                //2 获取sp的编辑器
                SharedPreferences sp = getSharedPreferences("data", MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                //3 put
                editor.putString("serverip",ip);
                //4 commit
                editor.commit();

            }
        });

        mBtnLogin  = (Button) findViewById(R.id.id_btn_login);

        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建线程
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        login();
                    }
                }).start();

            }
        });

        //TODO 加载并显示图片
            //获取 显示 图片的控件
        ImageView imageView = (ImageView) findViewById(R.id.id_iv_image);
            //加载显示
        Picasso.with(this)
                .load("http://"+getServerIp()+":8080/RestaurantServer/uploadFoodImage/guilin.jpg")
                .into(imageView);




    }

    private void login() {
        //利用OKHttp3访问网络，传递数据，完成登录验证
        //1 构建OKHttpClient对象
        //OkHttpClient client = new OkHttpClient();
        OkHttpClient client = new OkHttpClient.Builder()
                .cookieJar(new MyCookieJar())
                .build();

        //2 构建RequestBody对象。FormBody
        FormBody formBody = new FormBody.Builder()
                .add("userName","tom")
                .add("userPassword","123")
                .build();
        //3 构建Request对象
        Request request = new Request.Builder()
                .url("http://"+getServerIp()+":8080/RestaurantServer/user/doLogin2")
                .post(formBody)
                .build();

        //4 发送请求
        try {
            Response response = client.newCall(request).execute();
            //获取返回的字符串，保存到data中
            String data = response.body().string();
            Log.e(TAG, "onClick: "+data );

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent();
                    intent.setClass(LoginActivity.this,MainActivity.class);
                    startActivity(intent);

                }
            });

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String getServerIp() {
        //获取SP对象
        SharedPreferences sp = getSharedPreferences("data",MODE_PRIVATE);
        //获取sp中的key为serverip的值
        String serverip = sp.getString("serverip", null);
        //返回获取到的数据
        return serverip;
    }
}
